package com.flycms.web.project;

import com.flycms.core.entity.PageVo;
import com.flycms.core.entity.RestDataVo;
import com.flycms.module.project.model.Project;
import com.flycms.module.project.param.ProjectParam;
import com.flycms.module.project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 分页查询
     * @param param 查询参数
     * @return 返回分页参数
     */
    @GetMapping("/pro/page")
    public RestDataVo page(ProjectParam param){
        PageVo<Project> page = projectService.page(param);
        return RestDataVo.PAGE(page.getList(),page.getPageNum(),param.getPageSize(),page.getCount());
    }

    /**
     * 新增项目
     * @param project 项目信息
     * @return 新增结果
     */
    @PostMapping("/pro/save")
    public RestDataVo save(@RequestBody Project project){
        try{
            projectService.save(project);
        } catch (Exception e){
            return RestDataVo.ERROR("保存异常");
        }
        return RestDataVo.OK("保存成功");
    }

    /**
     * 删除
     * @param id ID
     * @return 删除结果
     */
    @DeleteMapping("/pro/del/{id}")
    public RestDataVo del(@PathVariable("id") Long id){
        try{
            projectService.del(id);
        } catch (Exception e){
            return RestDataVo.ERROR("删除异常");
        }
        return RestDataVo.OK("删除成功");
    }

    /**
     * 更新
     * @param project 信息
     * @return 更新结果
     */
    @PutMapping("/pro/update")
    public RestDataVo update(@RequestBody Project project){
        try{
            projectService.update(project);
        } catch (Exception e){
            return RestDataVo.ERROR("更新异常");
        }
        return RestDataVo.OK("更新成功");
    }

    /**
     * 分页查询
     * @param param 查询参数
     * @return 返回分页参数
     */
    @GetMapping("/system/pro/page")
    public RestDataVo sysPage(ProjectParam param){
        PageVo<Project> page = projectService.page(param);
        return RestDataVo.PAGE(page.getList(),param.getPageIndex(),param.getPageSize(),page.getCount());
    }

    /**
     * 新增项目
     * @param project 项目信息
     * @return 新增结果
     */
    @PostMapping("/system/pro/save")
    public RestDataVo sysSave(@RequestBody Project project){
        try{
            projectService.save(project);
        } catch (Exception e){
            return RestDataVo.ERROR("保存异常");
        }
        return RestDataVo.OK("保存成功");
    }

    /**
     * 删除
     * @param id ID
     * @return 删除结果
     */
    @DeleteMapping("/system/pro/del/{id}")
    public RestDataVo sysDel(@PathVariable("id") Long id){
        try{
            projectService.del(id);
        } catch (Exception e){
            return RestDataVo.ERROR("删除异常");
        }
        return RestDataVo.OK("删除成功");
    }

    /**
     * 更新
     * @param project 信息
     * @return 更新结果
     */
    @PutMapping("/system/pro/update")
    public RestDataVo sysUpdate(@RequestBody Project project){
        try{
            projectService.update(project);
        } catch (Exception e){
            return RestDataVo.ERROR("更新异常");
        }
        return RestDataVo.OK("更新成功");
    }

}
